<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小额贷款 - 申请贷款</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <nav class="navbar navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">
                <i class="fas fa-money-bill-wave me-2"></i>小额贷款
            </a>
            <div>
                <a href="/loan-status" class="btn btn-outline-light me-2">我的贷款</a>
                <a href="#" class="btn btn-outline-light" onclick="logout()">退出</a>
            </div>
        </div>
    </nav>

    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-body">
                        <h3 class="card-title text-center mb-4">申请贷款</h3>
                        <form id="loanForm">
                            <div class="mb-3">
                                <label for="amount" class="form-label">贷款金额（元）</label>
                                <input type="number" class="form-control" id="amount" min="1000" max="10000" step="100" required>
                                <div class="form-text">可贷金额：1000-10000元</div>
                            </div>
                            <div class="mb-3">
                                <label for="period" class="form-label">贷款期限（月）</label>
                                <select class="form-select" id="period" required>
                                    <option value="">请选择期限</option>
                                    <option value="3">3个月</option>
                                    <option value="6">6个月</option>
                                    <option value="12">12个月</option>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label for="purpose" class="form-label">贷款用途</label>
                                <select class="form-select" id="purpose" required>
                                    <option value="">请选择用途</option>
                                    <option value="日常消费">日常消费</option>
                                    <option value="教育培训">教育培训</option>
                                    <option value="医疗健康">医疗健康</option>
                                    <option value="装修家居">装修家居</option>
                                    <option value="其他">其他</option>
                                </select>
                            </div>
                            <button type="submit" class="btn btn-primary w-100">提交申请</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
    <script>
        document.getElementById('loanForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const amount = document.getElementById('amount').value;
            const period = document.getElementById('period').value;
            const purpose = document.getElementById('purpose').value;
            
            try {
                const response = await fetch('/api/loan-apply', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ amount, period, purpose })
                });
                
                const data = await response.json();
                
                if (data.success) {
                    alert('申请已提交，请等待审批！');
                    window.location.href = '/loan-status';
                } else {
                    alert(data.message);
                }
            } catch (error) {
                alert('申请失败，请重试');
            }
        });

        async function logout() {
            try {
                await fetch('/logout');
                window.location.href = '/';
            } catch (error) {
                window.location.href = '/';
            }
        }
    </script>
</body>
</html>